package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dubbo.api.UserInfoApi;
import com.itheima.dubbo.api.mongo.MovementApi;
import com.itheima.dubbo.api.mongo.VideoApi;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ManagerService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    public PageResult findAllUsers(Integer page, Integer pagesize) {
       Page<UserInfo> pageInfo =  userInfoApi.findByPage(page,pagesize);
       PageResult  pageResult = new PageResult(pageInfo.getCurrent(),pageInfo.getSize(),pageInfo.getTotal(),pageInfo.getRecords());
       return pageResult;
    }

    public ResponseEntity findById(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        return ResponseEntity.ok(userInfo);
    }

    public PageResult findAllVideos(Integer page, Integer pagesize, Long uid) {
        //1. 得到视频的分页对象
        PageResult pageResult = videoApi.findByUid(uid,page,pagesize);
        //2. 得到视频分页对象的Items，
        List<Video> videoList = (List<Video>) pageResult.getItems();

        //3. 提出视频发布人的id
        List<Long> userIds = CollUtil.getFieldValues(videoList, "userId", Long.class);
        Map<Long, UserInfo> userInfoMap = userInfoApi.findByIds(userIds);

        //4. 遍历视频列表，构造VideoVo对象
        List<VideoVo> voList = new ArrayList<>();
        for (Video video : videoList) {
            UserInfo userInfo = userInfoMap.get(video.getUserId());
            VideoVo videoVo = VideoVo.init(userInfo, video);
            //如果当前用户已经关注过视频的作者了，那么应该要设置为1
            voList.add(videoVo);
        }
        pageResult.setItems(voList);
        //5. 返回
        return pageResult;
    }

    public PageResult findAllMovements(Integer page, Integer pagesize, Long uid, Integer state) {
        //1.获取当前的登陆者的id，查询得到分页对象 PageResult<Movement>
        PageResult pageResult = movementApi.findFriendMovements(page,pagesize,uid);
        //2.提取UserId
        List<Movement> movementList = (List<Movement>) pageResult.getItems();
        List<Long> userIds = CollUtil.getFieldValues(movementList, "userId", Long.class);
        //3. 批量查询UserInfo对象
        Map<Long, UserInfo> userInfoMap = userInfoApi.findByIds(userIds);
        List<MovementsVo> voList = new ArrayList<>();
        //4. 遍历movemList，构建MoveMentVo
        for (Movement movement : movementList) {
            UserInfo userInfo = userInfoMap.get(movement.getUserId());
            MovementsVo vo = MovementsVo.init(userInfo, movement);
            voList.add(vo);
        }
        pageResult.setItems(voList);
        return pageResult;

    }
}
